package com.oocl.hgl.crm.service.impl;

import java.io.Serializable;
import java.util.List;

import com.oocl.hgl.crm.entity.Person;
import com.oocl.hgl.crm.service.PersionService;
import com.oocl.hgl.crm.store.PersonInfoStorer;
import com.oocl.hgl.crm.util.DoubleLinkedList;
import com.oocl.hgl.crm.util.Node;

public class PersonServiceImpl implements PersionService {
	
	private DoubleLinkedList<Person> pList;
	public PersonServiceImpl(){
		pList = PersonInfoStorer.pList;
	}
	public DoubleLinkedList<Person> list() {
		return pList;
	}
	public DoubleLinkedList<Person> queryByName(String name) {
		DoubleLinkedList<Person> rsList = new DoubleLinkedList<>();
		for (int i = 0; i < pList.size(); i++) {
			Person p = pList.get(i);
//			System.out.println(p.getName()==name);
//			System.out.println(p.getName().indexOf(name)>=0);
			if(p.getName()==name || p.getName().indexOf(name)>=0 ){
				rsList.insert_tail(p);
			}
		}
		return rsList;
	}
	public Person add(Person p) {
		if(p!=null && p.getName()!=null && p.getName().trim().length()>0){
			//
			PersonInfoStorer.updateCurrentId();
			p.setId( Person.currentId );
			//
			pList.insert_tail(p);
			return p;
		}
		return null;
	}
	public Person delete(Serializable id) {
		 Person p = get(id);
		 for (int i = 0; i < pList.size(); i++) {
				Person tmp = pList.get(i);
				if(p==tmp){
					pList.delNode(p);
					return p;
				}
			}
		return null;
	}
	public Person update(Serializable id, Person p) {
		Person tmpP = get(id);
		
		return null;
	}
	@Override
	public Person get(Serializable id) {
		for (int i = 0; i < pList.size(); i++) {
			Person p = pList.get(i);
			Serializable tmpId = p.getId(); 
			if(p!=null && tmpId==id){
				return p;
			}
		}
		return null;
	}
	@Override
	public DoubleLinkedList<Person> sortByName() {
		//
		Person.sort = Person.COMPARE_BY_NAME;
		DoubleLinkedList<Person> rsList = new DoubleLinkedList<>();
		// 
		Person[] pArr = new Person[pList.size()];
		for (int i = 0; i < pList.size(); i++) {
			pArr[i] = pList.get(i);
		}
		
		 int tmpIdx;
		 for(int i=0;i<pArr.length-1;i++){
			 tmpIdx = i;
			 for(int j=i+1;j<pArr.length;j++){
				 if(pArr[tmpIdx].compareTo(pArr[j])>0){
					 tmpIdx = j;
				 }
			 }
			 if(tmpIdx!=i){
				 Person tmpValue = pArr[tmpIdx]; 
				 pArr[tmpIdx] = pArr[i];
				 pArr[i] = tmpValue;
			 }
		 }
		
		//
		for (int i = 0; i < pList.size(); i++) {
			rsList .insert_tail( pArr[i] );
		}
		return rsList;
	}
	@Override
	public DoubleLinkedList<Person> sortById() {
		//
		Person.sort = Person.COMPARE_BY_ID;
		DoubleLinkedList<Person> rsList = new DoubleLinkedList<>();
		// 
		Person[] pArr = new Person[pList.size()];
		for (int i = 0; i < pList.size(); i++) {
			pArr[i] = pList.get(i);
		}
		
		 int tmpIdx;
		 for(int i=0;i<pArr.length-1;i++){
			 tmpIdx = i;
			 for(int j=i+1;j<pArr.length;j++){
				 if(pArr[tmpIdx].compareTo(pArr[j])>0){
					 tmpIdx = j;
				 }
			 }
			 if(tmpIdx!=i){
				 Person tmpValue = pArr[tmpIdx]; 
				 pArr[tmpIdx] = pArr[i];
				 pArr[i] = tmpValue;
			 }
		 }
		
		//
		for (int i = 0; i < pList.size(); i++) {
			rsList .insert_tail( pArr[i] );
		}
		return rsList;
	}
	


}
